package com.jhclothing.web.controller.inbound;

import com.jhclothing.common.annotation.Log;
import com.jhclothing.common.core.controller.BaseController;
import com.jhclothing.common.core.domain.AjaxResult;
import com.jhclothing.common.core.page.TableDataInfo;
import com.jhclothing.common.enums.BusinessType;
import com.jhclothing.common.utils.StringUtils;
import com.jhclothing.common.utils.poi.ExcelUtil;
import com.jhclothing.inbound.domain.ClothingInbound;
import com.jhclothing.inbound.service.IClothingInboundService;
import com.jhclothing.product.domain.ClothingType;
import com.jhclothing.product.service.IClothingTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 服饰入库Controller
 *
 * @author zyp
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/inbound/clothing")
public class ClothingInboundController extends BaseController {
    @Autowired
    private IClothingInboundService clothingInboundService;
    @Autowired
    private IClothingTypeService clothingTypeService;

    /**
     * 查询服饰入库列表
     */
    @PreAuthorize("@ss.hasPermi('inbound:clothing:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClothingInbound clothingInbound) {
        startPage();
        List<ClothingInbound> list = clothingInboundService.selectClothingInboundList(clothingInbound);
        return getDataTable(list);
    }

    /**
     * 导出服饰入库列表
     */
    @PreAuthorize("@ss.hasPermi('inbound:clothing:export')")
    @Log(title = "服饰入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClothingInbound clothingInbound) {
        List<ClothingInbound> list = clothingInboundService.selectClothingInboundList(clothingInbound);
        ExcelUtil<ClothingInbound> util = new ExcelUtil<>(ClothingInbound.class);
        util.exportExcel(response, list, "服饰入库数据");
    }

    /**
     * 获取服饰入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('inbound:clothing:query')")
    @GetMapping(value = {"/", "/{clothingInboundId}"})
    public AjaxResult getInfo(@PathVariable(value = "clothingInboundId", required = false) Long clothingInboundId) {
        AjaxResult ajax = AjaxResult.success();
        List<ClothingType> clothingTypes = clothingTypeService.selectClothingTypeAll();
        ajax.put("clothingTypes", clothingTypes);
        if (StringUtils.isNotNull(clothingInboundId)) {
            ClothingInbound clothingInbound = clothingInboundService.selectClothingInboundByInboundId(clothingInboundId);
            ajax.put(AjaxResult.DATA_TAG, clothingInbound);
            ajax.put("clothingTypeId", clothingInbound.getClothingInventory().getClothingTypeId());
            ajax.put("clothingSize", clothingInbound.getClothingInventory().getClothingSize());
        }
        return ajax;
    }

    /**
     * 新增服饰入库
     */
    @PreAuthorize("@ss.hasPermi('inbound:clothing:add')")
    @Log(title = "服饰入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClothingInbound clothingInbound) {
        clothingInbound.setCreateBy(getUsername());
        String result = clothingInboundService.insertClothingInbound(clothingInbound);
        if (!"SUCCESS".equals(result)) {
            return error(result);
        }
        return AjaxResult.success();
    }

    /**
     * 修改服饰入库
     */
    @PreAuthorize("@ss.hasPermi('inbound:clothing:edit')")
    @Log(title = "服饰入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClothingInbound clothingInbound) {
        clothingInbound.setUpdateBy(getUsername());
        String result = clothingInboundService.updateClothingInbound(clothingInbound);
        if (!"SUCCESS".equals(result)) {
            return error(result);
        }
        return AjaxResult.success();
    }

    /**
     * 删除服饰入库
     */
    @PreAuthorize("@ss.hasPermi('inbound:clothing:remove')")
    @Log(title = "服饰入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clothingInboundIds}")
    public AjaxResult remove(@PathVariable Long[] clothingInboundIds) {
        String result = clothingInboundService.deleteClothingInboundByInboundIds(clothingInboundIds, getUsername());
        if (!"SUCCESS".equals(result)) {
            return error(result);
        }
        return AjaxResult.success();
    }
}
